package com.dqyt.khjc.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.Report;
import com.dqyt.app.mongo.ReportDao;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.SmsRecordBean;
import com.dqyt.khjc.bean.User;
import com.dqyt.khjc.mapper.SmsRecordMapper;
import com.dqyt.khjc.sms.SMSConfig;
import com.dqyt.khjc.sms.SMSParm;
import com.dqyt.khjc.sms.SMSTool;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class SmsRecordService {

    @Autowired
    private SmsRecordMapper smsRecordMapper;

    @Autowired
    private ReportDao reportDao;

    /**
     * 发送短信
     * @param request
     * @param smapIds
     * @param smsContent
     * @param targetPhone
     * @return
     */
    public ResultMsg sendSmsToCon(HttpServletRequest request, String [] smapIds,String smsContent,String targetPhone) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值






            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            SMSParm mSMSParm = new SMSParm();
            mSMSParm.TemplateID = SMSConfig.NOTICE_SMS; // 模板ID
            String[] a = { "+86" + targetPhone }; // 发送对象电话号码
            mSMSParm.PhoneNumberSet = a;
            String[] parm = {smsContent}; // 模板参数
            mSMSParm.TemplateParamSet = parm;
            SendSmsResponse smsRet = SMSTool.sendSMS(JSON.toJSONString(mSMSParm));
            if (SMSTool.isSendSMSOK(smsRet)) {
                // 插入數據庫
                SmsRecordBean smsRecordBean = new SmsRecordBean();
                smsRecordBean.setSmsContent(smsContent);
                smsRecordBean.setSenderId(user.getUserId());
                smsRecordBean.setTargetPhone(targetPhone);
                String smapIdsStr="";
                for(String str:smapIds){
                    smapIdsStr +=str+",";
                }
                smsRecordBean.setSmapIds(smapIdsStr);
                smsRecordMapper.addSmsRecord(smsRecordBean);

                for(String bh:smapIds){
                    Report report =  reportDao.getReportByBH(bh);
                    report.isSendSMS = 1;
                    reportDao.save(report);
                }

                // 发送成功
                ret.setMsg("短信发送成功！");
                ret.setCode(ResultMsg.SUCESS);

            } else {
                // 发送失败
                ret.setMsg("连接异常！");
                ret.setCode(ResultMsg.ERROR);
            }
        } catch (Exception e) {
        ret.setCode(ResultMsg.ERROR);
        ret.setMsg("未知的错误，请稍候再试！");
        e.printStackTrace();
    }
        return ret;

    }
}
